﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MySql.Data.MySqlClient;

namespace home_material_tool
{
    public class CommonSql
    {
        public static MySqlConnection MysqlConn = new MySqlConnection();

        public static readonly string DB = "hm_video";
        public static readonly string MovieTableName = "hm_movie";
        public static readonly string TVSeriesTableName = "hm_tv_series";
        public static string ConnectionString = string.Empty;
        //50 records/page
        public static int RecCountPerPage = 50;
        public static async void AsyncCloseDB()
        {
            try
            {
                //add await just for throwing exception
                //https://learn.microsoft.com/en-us/dotnet/api/system.data.common.dbconnection.openasync?view=net-8.0
                await MysqlConn.CloseAsync();
                await MysqlConn.DisposeAsync();
            }
            catch(MySqlException ex)
            {

            }
        }

        public static async Task<string> AsyncOpenDB()
        {
            string errStr = string.Empty;
            if (string.IsNullOrEmpty(ConnectionString))
            {
                return "数据库连接字符串未设";
            }
            try
            {
                //add await just for throwing exception
                //https://learn.microsoft.com/en-us/dotnet/api/system.data.common.dbconnection.openasync?view=net-8.0
                //MysqlConn.ConnectionString will changed after opening
                MysqlConn.ConnectionString = ConnectionString;
                await MysqlConn.OpenAsync();
            }
            catch (MySqlException ex)
            {
                errStr = ex.Message;
            }
            return errStr;
        }

        public static void InsertDB(string tableName, List<string> fieldNameList, List<string> fieldVauleList, string where)
        {

        }


    }
}
